from helpers import * #@UnusedWildImport
from MenuItem import * #@UnusedWildImport

class MainMenu(object):


    def __init__(self):
        self.returning=False
        self.menu_items = pygame.sprite.RenderUpdates()
        
        total=6
        cont = MenuItem(2, total, None, 'menu\Continue.png')
        newgame = MenuItem(3, total, None, 'menu\NewGame.png')
        loadgame = MenuItem(4, total, None, 'menu\LoadGame.png')
        leveledit = MenuItem(5, total, None, 'menu\Editor.png')
        quit = MenuItem(6, total, None, 'menu\Quit.png')
        
        self.select = Selector(total)
        items=[newgame, cont, loadgame, leveledit, quit, self.select]
        for item in items:
            self.menu_items.add(item) 
            
        self.choice = 0
        
    def inGameMenu(self):
        self.choice = 0
        if self.menu_items.__len__() is 6:
            save = MenuItem(1, 6, None, 'menu/Save.png')
            self.menu_items.add(save)
            self.select.addSaveOption()
        
    def handleEvents(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                return False
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    return False
                self.keyPress(event.key)
        return True
    
    def update(self):
        for item in self.menu_items:
            item.update()
        if self.choice is 5:
            self.choice = 0
            return 5
        return self.choice
    
    def display(self, screen):
        screen.fill((0,0,0))
        self.menu_items.draw(screen)      
    
    def keyPress(self, key):
        if key == K_DOWN:
            self.select.cycle("down")
        if key == K_UP:
            self.select.cycle("up")
        if key == K_RETURN:
            self.choose(self.select.selected())
    
    def choose(self, choice):
        self.choice = choice

class Selector(pygame.sprite.Sprite):

    def __init__(self, total = 2, number = 2, image = None, path = 'menu\Selector.png'):
        pygame.sprite.Sprite.__init__(self) 
        
        if image == None:
            self.image, self.rect = load_image(path,(0,0,0))
        else:
            self.image = image
            self.rect = image.get_rect()
        self.current = number
        self.min = 2
        self.total = total
        self.x_pos=380 - (15*number-1) - self.rect.width
        self.y_pos=104 + (250/total)*number 
        
        self.rect.topleft = [self.x_pos, self.y_pos]
    
    def addSaveOption(self):
        self.min = 1
        
    def update(self):
        self.x_pos=380 - (15*self.current-1) - self.rect.width
        self.y_pos=104 + (250/self.total)*self.current 
        self.rect.topleft = [self.x_pos, self.y_pos]
        pass
    
    def cycle(self, direction):
        if direction is "up":
            self.current -= 1
        elif direction is "down":
            self.current +=1
        if self.current < self.min:
            self.current = self.total
        elif self.current > self.total:
            self.current = self.min
    
    def selected(self):
        return self.current        